<?php

/**
 * review actions.
 *
 * @package    Bookstore
 * @subpackage review
 * @author     Enkuso
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class reviewActions extends sfActions
{

    public function preExecute()
    {
        parent::preExecute();

        /**
         * Хэрэглэгч өөрийн эрхээр нэвтэрсэн байх ёстой.
         */
        $this->forward404Unless($this->getRequest()->hasParameter('token'));
    }

    /**
     * Бүтээлд сэтгэгдэл бичиж, сэтгэгдлийн үнэлгээ өгөх
     */
    public function executeAddReview(sfWebRequest $request)
    {
        $this->forward404Unless($request->isMethod('POST'));

        $book_id = $request->getParameter('book_id');
        $reveiw = $request->getParameter('review');
        $rate = $request->getParameter('rate');

        $form = new reviewForm(array(),array(),false);

        $form->bind(array(
            'book_id'   => $book_id,
            'review'    => $reveiw,
            'rate'      => $rate,
            'user_id'   => $this->getUser()->getGuardUser()->getId()
        ));
        if($form->isValid()){
            $book_review = new BookRate();
            $book_review->setBookId($form->getValue('book_id'));
            $book_review->setUserId($form->getValue('user_id'));
            $book_review->setRate($form->getValue('rate'));
            $book_review->setComment($form->getValue('review'));
            $book_review->replace();
            $this->stat = 'ok';
            $this->review = $book_review->asArray();
        } else {
            if($form['book_id']->hasError())
                $this->error_code = 1;
            if($form['review']->hasError())
                $this->error_code = 8;
            if($form['rate']->hasError())
                $this->error_code = 9;
            if($form['user_id']->hasError())
                $this->error_code = 98;
            $this->error_msg = apiErrors::getErrorMessage($this->error_code);
            $this->stat = 'fail';
        }
    }

    /**
     * Хэрэглэгч өөрийн бичсэн сэтгэгдэл, үнэлгээг устгах
     */
    public function executeDeleteReview(sfWebRequest $request)
    {
        $this->forward404Unless($request->isMethod('POST') || $request->isMethod('DELETE'));

        $book_id = $request->getParameter('book_id');
        $user = $this->getUser()->getGuardUser();

        $review = BookRateTable::getInstance()->findReviewByBookAndUser($book_id, $user->getId());

        if($review){
            $review->delete();

            $this->stat = 'ok';
        } else {
            $this->stat = 'fail';
            $this->error_code = 7;
            $this->error_msg = apiErrors::getErrorMessage($this->error_code);
        }
    }

    /**
     * Хэрэглэгч өөрийн бичсэн сэтгэгдэл, үнэлгээг засах
     */
    public function executeEditReview(sfWebRequest $request)
    {
        $this->forward404Unless($request->isMethod('POST') || $request->isMethod('PUT'));

        $book_id = $request->getParameter('book_id');
        $user = $this->getUser()->getGuardUser();
        $comment = $request->getParameter('review');
        $rate = $request->getParameter('rate');

        $review = BookRateTable::getInstance()->findReviewByBookAndUser($book_id, $user->getId());

        if($review){
            $form = new reviewForm(array(),array(),false);

            $form->bind(array(
                'book_id'   => $book_id,
                'review'    => $comment,
                'rate'      => $rate,
                'user_id'   => $user->getId()
            ));
            if($form->isValid()){
                $review->setRate($form->getValue('rate'));
                $review->setComment($form->getValue('review'));
                $review->save();

                $this->stat = 'ok';
            } else {
                if($form['book_id']->hasError())
                    $this->error_code = 1;
                if($form['review']->hasError())
                    $this->error_code = 8;
                if($form['rate']->hasError())
                    $this->error_code = 9;
                if($form['user_id']->hasError())
                    $this->error_code = 98;
                $this->error_msg = apiErrors::getErrorMessage($this->error_code);
                $this->stat = 'fail';
            }
        } else {
            $this->stat = 'fail';
            $this->error_code = 7;
            $this->error_msg = apiErrors::getErrorMessage($this->error_code);
        }
    }
}
